package com.cat.dataStructure;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/max-sum-of-a-pair-with-equal-sum-of-digits/
 * @create 2025/8/15 17:22
 * @since JDK17
 */

public class Solution19 {
    int f(int num) {
        int ans = 0;
        while (num != 0) {
            ans += num % 10;
            num /= 10;
        }
        return ans;
    }

    static HashMap<Integer, Integer> map = new HashMap<>();
    public int maximumSum(int[] nums) {
        map.clear();
        int ans = -1, n = nums.length;
        for (int i = 0, t, k; i < n; i++) {
            k = f(nums[i]);
            t = map.getOrDefault(k, 0);
            if (t != 0) {
                ans = Math.max(ans, t + nums[i]);
            }
            if (t < nums[i]) {
                map.put(k, nums[i]);
            }
        }

        return ans;
    }
}
